#!/usr/bin/python2.5
# -*- coding: us-ascii -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab

"""
iSCSI protocol constants

"""

DRAFT20_VERSION = 0x00

# default iSCSI listen port for incoming connections
LISTEN_PORT = 3260

# Padding word length
PAD_LEN = 4


RESERVED_TAG = 0xffffffff

##  Opcode encoding bits 
OP_RETRY = 0x80
OP_IMMEDIATE = 0x40
OPCODE_MASK = 0x3F

##  Initiator Opcode values 
OP_NOOP_OUT = 0x00
OP_SCSI_CMD = 0x01
OP_SCSI_TMFUNC = 0x02
OP_LOGIN = 0x03
OP_TEXT = 0x04
OP_SCSI_DATA_OUT = 0x05
OP_LOGOUT = 0x06
OP_SNACK = 0x10

OP_VENDOR1_CMD = 0x1c
OP_VENDOR2_CMD = 0x1d
OP_VENDOR3_CMD = 0x1e
OP_VENDOR4_CMD = 0x1f

##  Target Opcode values 
OP_NOOP_IN = 0x20
OP_SCSI_CMD_RSP = 0x21
OP_SCSI_TMFUNC_RSP = 0x22
OP_LOGIN_RSP = 0x23
OP_TEXT_RSP = 0x24
OP_SCSI_DATA_IN = 0x25
OP_LOGOUT_RSP = 0x26
OP_R2T = 0x31
OP_ASYNC_EVENT = 0x32
OP_REJECT = 0x3f


AHSTYPE_CDB = 1
AHSTYPE_RLENGTH = 2
CDB_SIZE = 16

##  Command PDU flags 
FLAG_CMD_FINAL = 0x80
FLAG_CMD_READ = 0x40
FLAG_CMD_WRITE = 0x20
FLAG_CMD_ATTR_MASK = 0x07   #  3 bits 

##  SCSI Command Attribute values 
ATTR_UNTAGGED = 0
ATTR_SIMPLE = 1
ATTR_ORDERED = 2
ATTR_HEAD_OF_QUEUE = 3
ATTR_ACA = 4

##  Command Response PDU flags 
FLAG_CMD_BIDI_OVERFLOW = 0x10
FLAG_CMD_BIDI_UNDERFLOW = 0x08
FLAG_CMD_OVERFLOW = 0x04
FLAG_CMD_UNDERFLOW = 0x02

##  iSCSI Status values. Valid if Rsp Selector bit is not set 
STATUS_CMD_COMPLETED = 0
STATUS_TARGET_FAILURE = 1
STATUS_SUBSYS_FAILURE = 2

##  iSCSI Event Codes 
ASYNC_MSG_SCSI_EVENT = 0
ASYNC_MSG_REQUEST_LOGOUT = 1
ASYNC_MSG_DROPPING_CONNECTION = 2
ASYNC_MSG_DROPPING_ALL_CONNECTIONS = 3
ASYNC_MSG_PARAM_NEGOTIATION = 4
ASYNC_MSG_VENDOR_SPECIFIC = 255

FLAG_TM_FUNC_MASK = 0x7F

##  Function values 
TM_FUNC_ABORT_TASK = 1
TM_FUNC_ABORT_TASK_SET = 2
TM_FUNC_CLEAR_ACA = 3
TM_FUNC_CLEAR_TASK_SET = 4
TM_FUNC_LOGICAL_UNIT_RESET = 5
TM_FUNC_TARGET_WARM_RESET = 6
TM_FUNC_TARGET_COLD_RESET = 7
TM_FUNC_TASK_REASSIGN = 8

#TM_FUNC_VALUE(hdr) ((hdr)->flags & ISCSI_FLAG_TM_FUNC_MASK)


##  Response values 
TMF_RSP_COMPLETE = 0x00
TMF_RSP_NO_TASK = 0x01
TMF_RSP_NO_LUN = 0x02
TMF_RSP_TASK_ALLEGIANT = 0x03
TMF_RSP_NO_FAILOVER = 0x04
TMF_RSP_NOT_SUPPORTED = 0x05
TMF_RSP_AUTH_FAILED = 0x06
TMF_RSP_REJECTED = 0xff

##  Data Response PDU flags 
FLAG_DATA_ACK = 0x40
FLAG_DATA_OVERFLOW = 0x04
FLAG_DATA_UNDERFLOW = 0x02
FLAG_DATA_STATUS = 0x01

FLAG_TEXT_CONTINUE = 0x40

##  Login PDU flags 
FLAG_LOGIN_TRANSIT = 0x80
FLAG_LOGIN_CONTINUE = 0x40
FLAG_LOGIN_CURRENT_STAGE_MASK = 0x0C    #  2 bits 
FLAG_LOGIN_NEXT_STAGE_MASK = 0x03   #  2 bits 

##  Login stage (phase) codes for CSG, NSG 
INITIAL_LOGIN_STAGE = -1
SECURITY_NEGOTIATION_STAGE = 0
OP_PARMS_NEGOTIATION_STAGE = 1
FULL_FEATURE_PHASE = 3

##  Login Status response classes 
STATUS_CLS_SUCCESS = 0x00
STATUS_CLS_REDIRECT = 0x01
STATUS_CLS_INITIATOR_ERR = 0x02
STATUS_CLS_TARGET_ERR = 0x03

##  Login Status response detail codes 
##  Class-0 (Success) 
LOGIN_STATUS_ACCEPT = 0x00

##  Class-1 (Redirection) 
LOGIN_STATUS_TGT_MOVED_TEMP = 0x01
LOGIN_STATUS_TGT_MOVED_PERM = 0x02

##  Class-2 (Initiator Error) 
LOGIN_STATUS_INIT_ERR = 0x00
LOGIN_STATUS_AUTH_FAILED = 0x01
LOGIN_STATUS_TGT_FORBIDDEN = 0x02
LOGIN_STATUS_TGT_NOT_FOUND = 0x03
LOGIN_STATUS_TGT_REMOVED = 0x04
LOGIN_STATUS_NO_VERSION = 0x05
LOGIN_STATUS_ISID_ERROR = 0x06
LOGIN_STATUS_MISSING_FIELDS = 0x07
LOGIN_STATUS_CONN_ADD_FAILED = 0x08
LOGIN_STATUS_NO_SESSION_TYPE = 0x09
LOGIN_STATUS_NO_SESSION = 0x0a
LOGIN_STATUS_INVALID_REQUEST = 0x0b

##  Class-3 (Target Error) 
LOGIN_STATUS_TARGET_ERROR = 0x00
LOGIN_STATUS_SVC_UNAVAILABLE = 0x01
LOGIN_STATUS_NO_RESOURCES = 0x02

##  Logout PDU flags 
FLAG_LOGOUT_REASON_MASK = 0x7F

##  logout reason_code values 

LOGOUT_REASON_CLOSE_SESSION = 0
LOGOUT_REASON_CLOSE_CONNECTION = 1
LOGOUT_REASON_RECOVERY = 2
LOGOUT_REASON_AEN_REQUEST = 3

##  logout response status values 

LOGOUT_SUCCESS = 0
LOGOUT_CID_NOT_FOUND = 1
LOGOUT_RECOVERY_UNSUPPORTED = 2
LOGOUT_CLEANUP_FAILED = 3

##  SNACK PDU flags 
FLAG_SNACK_TYPE_MASK = 0x0F ##  4 bits 

##  Reason for Reject 
REASON_CMD_BEFORE_LOGIN = 1
REASON_DATA_DIGEST_ERROR = 2
REASON_DATA_SNACK_REJECT = 3
REASON_PROTOCOL_ERROR = 4
REASON_CMD_NOT_SUPPORTED = 5
REASON_IMM_CMD_REJECT = 6
REASON_TASK_IN_PROGRESS = 7
REASON_INVALID_SNACK = 8
REASON_BOOKMARK_INVALID = 9
REASON_BOOKMARK_NO_RESOURCES = 10
REASON_NEGOTIATION_RESET = 11

##  Max. number of Key=Value pairs in a text message 
MAX_KEY_VALUE_PAIRS = 8192

##  maximum length for text keys/values 
KEY_MAXLEN = 64
VALUE_MAXLEN = 255
TARGET_NAME_MAXLEN = VALUE_MAXLEN

DEF_MAX_RECV_SEG_LEN = 8192
MIN_MAX_RECV_SEG_LEN = 512
MAX_MAX_RECV_SEG_LEN = 16777215

DEF_FIRST_BURST_LEN = 65536
MIN_FIRST_BURST_LEN = 512
MAX_FIRST_BURST_LEN = 16777215

DEF_MAX_BURST_LEN = 262144
MIN_MAX_BURST_LEN = 512
MAX_MAX_BURST_LEN = 16777215

DEF_TIME2WAIT = 2


